package com.ruoyi.ring.service.impl;

import com.ruoyi.common.enums.dict.AiModel;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ora.constant.BizConstants;
import com.ruoyi.ora.domain.ChatRequest;
import com.ruoyi.ora.domain.ChatResponse;
import com.ruoyi.ring.domain.RingDeviceAichat;
import com.ruoyi.ring.factory.AiFactory;
import com.ruoyi.ring.mapper.RingDeviceAichatMapper;
import com.ruoyi.ring.service.IRingDeviceAichatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * AI聊天Service业务层处理
 * 
 * @author liuwp
 * @date 2025-10-11
 */
@Service
public class RingDeviceAichatServiceImpl implements IRingDeviceAichatService 
{
    @Autowired
    private RingDeviceAichatMapper ringDeviceAichatMapper;

    /**
     * 查询AI聊天
     * 
     * @param id AI聊天主键
     * @return AI聊天
     */
    @Override
    public RingDeviceAichat selectRingDeviceAichatById(Long id)
    {
        return ringDeviceAichatMapper.selectRingDeviceAichatById(id);
    }

    /**
     * 查询AI聊天列表
     * 
     * @param ringDeviceAichat AI聊天
     * @return AI聊天
     */
    @Override
    public List<RingDeviceAichat> selectRingDeviceAichatList(RingDeviceAichat ringDeviceAichat)
    {
        return ringDeviceAichatMapper.selectRingDeviceAichatList(ringDeviceAichat);
    }

    /**
     * 根据业务主键查询一条记录：AI聊天
     *
     * @param ringDeviceAichat AI聊天
     * @return AI聊天
     */
    @Override
    public RingDeviceAichat selectOneRingDeviceAichat(RingDeviceAichat ringDeviceAichat)
    {
        return ringDeviceAichatMapper.selectOneRingDeviceAichat(ringDeviceAichat);
    }

    /**
     * 新增AI聊天
     * 
     * @param ringDeviceAichat AI聊天
     * @return 结果
     */
    @Override
    public int insertRingDeviceAichat(RingDeviceAichat ringDeviceAichat)
    {
        ringDeviceAichat.setCreateTime(DateUtils.getNowDate());
        return ringDeviceAichatMapper.insertRingDeviceAichat(ringDeviceAichat);
    }

    /**
     * 修改AI聊天
     * 
     * @param ringDeviceAichat AI聊天
     * @return 结果
     */
    @Override
    public int updateRingDeviceAichat(RingDeviceAichat ringDeviceAichat)
    {
        return ringDeviceAichatMapper.updateRingDeviceAichat(ringDeviceAichat);
    }

    @Override
    public int updateRingDeviceAichatCondition(RingDeviceAichat ringDeviceAichat, RingDeviceAichat ringDeviceAichatCondition)
    {
        return ringDeviceAichatMapper.updateRingDeviceAichatCondition(ringDeviceAichat, ringDeviceAichatCondition);
    }

    /**
     * 批量删除AI聊天
     * 
     * @param ids 需要删除的AI聊天主键
     * @return 结果
     */
    @Override
    public int deleteRingDeviceAichatByIds(Long[] ids)
    {
        return ringDeviceAichatMapper.deleteRingDeviceAichatByIds(ids);
    }

    /**
     * 删除AI聊天信息
     * 
     * @param id AI聊天主键
     * @return 结果
     */
    @Override
    public int deleteRingDeviceAichatById(Long id)
    {
        return ringDeviceAichatMapper.deleteRingDeviceAichatById(id);
    }

    /**
     * 条件删除AI聊天
     *
     * @param ringDeviceAichat AI聊天
     * @return 结果
     */
    @Override
    public int deleteRingDeviceAichatByCondition(RingDeviceAichat ringDeviceAichat)
    {
        return ringDeviceAichatMapper.deleteRingDeviceAichatByCondition(ringDeviceAichat);
    }
    @Override
    public RingDeviceAichat aichatAutoReply(List<RingDeviceAichat> deviceAichats) {

        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(AiModel.HUNYUAN.getValue());

        List<ChatRequest.Message> messages = new ArrayList<>();

        ChatRequest.Message sysMessage = new ChatRequest.Message();
        sysMessage.setRole("system");
        sysMessage.setContent("你是一名智能戒指（监测睡眠质量，压力，饮食，情绪，血氧，心率，心率变异性，锻炼，运动，生理周期，减压，助眠，健康计划等）的AI聊天机器人，请根据用户的问题，进行智能回复。并且要求字符在50字以内。");
        messages.add(sysMessage);

        for (RingDeviceAichat deviceAichat : deviceAichats) {
            ChatRequest.Message message = new ChatRequest.Message();
            message.setRole("user");
            if (deviceAichat.getChatType() == 1){
                message.setContent(deviceAichat.getDeviceContent());
            }else {
                message.setContent(deviceAichat.getAichatContent());
            }
            messages.add(message);
        }
        chatRequest.setMessages(messages);

        // 根据设备内容，调用AI自动回复
        ChatResponse chatResponse = AiFactory.build(BizConstants.AI_MODEL.getValue()).chatCompletions(chatRequest);
        if (chatResponse != null && chatResponse.getError() == null) {
            // 组装回复内容
            RingDeviceAichat aichat = new RingDeviceAichat();
            aichat.setAichatContent(chatResponse.getChoices().get(0).getMessage().getContent());
            aichat.setDeviceSn(deviceAichats.get(0).getDeviceSn());
            aichat.setChatType(2);
            return aichat;
        }
        return null;
    }
}
